<?php
namespace Home\Controller;
use Think\Controller;

class LessonController extends Controller
{
    //课程表界面
    public function index()
    {
        $lesson_list = M('Lesson')->select();
        //select * from lesson
        for ($i = 0; $i < 5; $i++) {
            for ($j = 0; $j < 7; $j++) {
                $lesson_array[$i][$j] = "";
            }
        }
        foreach ($lesson_list as $lesson) {
            $lesson_array[$lesson['row'] - 1][$lesson['colum'] - 1] = $lesson['name'];
        }
        $this->assign('sidebar', 'lesson');
        $this->assign('lesson_list', $lesson_array);
        $this->display('index');
    }
    //课程评估
    public function assess()
    {
        $std_id = session("std_id");
        if (strlen($std_id) != 0) {
            $lesson_list = M("Lesson")->select();
            $this->assign('lesson_list', $lesson_list);
            $this->assign('sidebar', 'lesson');
            $this->assign('std_id',$std_id);
            $this->display('assess');
        } else {
            $this->redirect('/Home/Index/index', array(), 0, '');
        }
    }
    //提交评估结果
    public function set_assess()
    {
        $lesson_list = rtrim(I('lesson_list'),',');
        $assess_list = rtrim(I('assess_list'),',');
        $lesson_array = explode(',', $lesson_list);
        $assess_array = explode(',', $assess_list);
        $i = 0;
        $j = 0;
        $std_id=session("std_id");
        foreach ($lesson_array as $lesson_info) {
            $data['std_id']=$std_id;
            $data['lesson_id'] = $lesson_info;
            $tf=M("Assess")->where($data)->getField('Id');
            //select Id from go_assess where std_id=$std_id and lesson_id=$lesson_info;
            $data['grade'] =$assess_array[$i] ;
            if(!empty($tf))
            {
                $result=M("Assess")->where(array('Id'=>$tf))->save($data);
                //update go_assess set grade=$assess_array[$i] where Id=$tf
                if( $result||$result===0 ){
                    ++$j;
                }
            }
            else
            {
                $assess = M("Assess")->create($data);
                //insert into go_assess (lesson_id,grade,std_id)value($lesson_info,$assess_array[$i],$std_id)
                if (is_array($assess)) {
                    $result=M("Assess")->data($data)->add();
                    if( $result )
                    {
                        ++$j;
                    }
                }
            }
            ++$i;
            unset($data);
        }
        if ($i == $j) {
            $return['status'] = 0;
            $return['info'] = "";
        } else {
            $return['status'] = 1;
            $return['info'] = "数据异常，请重新提交";
        }
        $this->ajaxReturn($return);
    }
}